<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
      vertical-align: middle;
      text-decoration: none;
      font-family: '微软雅黑';
      font-size: 14px;
      color: white;
    }
    .box{
      width: 590px;
      height: 470px;
      border: 1px solid #ccc;
      margin: 20px auto;
      overflow: hidden;
      position: relative;
    }
    .box ul{
      width: 3000px;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }
    li{
      float: left;
    }
    .btns{
      width:100%;
      height: 60px;
      position: absolute;
      top:50%;
      transform: translateY(-50%);
      display: none;
    }
    .btns a{
      display: block;
      width: 50px;
      height: 60px;
      background-color: rgba(0,0,0,0.5);
      text-align: center;
      line-height: 60px;
      float: left;
    }
    .btns a:last-child{
      float: right;
    }
    .dot{
      position: absolute;
      bottom: 10px;
      left: 16px;
    }
    .dot span{
      display: inline-block;
      width: 14px;
      height: 14px;
      border: 1px solid #ccc;
      border-radius: 50%;
      margin-right: 8px;
    }
    .active{
      background-color: white;
      box-shadow: 0 0 0px 5px rgba(255,255,255,0.5);
    }
  </style>
</head>
<body>
  <div class="box">
    <ul>
      <li><a href="http://www.jd.com"><img src="img/1.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/2.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/3.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/4.jpg" alt=""></a></li>
    </ul>
    <div class="btns">
      <a href="javastript:;">&lt;</a>
      <a href="javastript:;">&gt;</a>
    </div>
    <div class="dot">
      <!-- 动态添加小圆点span -->
    </div>
  </div>
  <script>
    //动态创建小圆点
    var dot = document.querySelector('.dot');
    var imgs = document.querySelectorAll('img');
    for(var i = 0; i < imgs.length; i++){
      var span = document.createElement('span');
      dot.appendChild(span);
      // dot.children[0] ? dot.children[0].classList.add('active') : dot.children[0].classList.remove('active');
      // i == 0 ? span.className = 'active' : span.className ='';
      i == 0 ? span.classList.add('active') : span.classList.remove('active');
      //点击小圆点跳到点击的页面
      span.onclick = span_click;
      span.myindex = i;
    }
    function span_click(){
      pageindex = this.myindex;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
      }
      dot.children[pageindex].className = 'active';
    }
    //右边按钮
    var rbtn = document.querySelector('.btns a:nth-child(2)');
    var ul = document.querySelector('.box ul');
    var pageindex = 0;
    rbtn.onclick = function(){
      if(pageindex == imgs.length){
        ul.style.left = 0 + 'px';
        pageindex = 0;
      }
      pageindex ++;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
        if(pageindex >= imgs.length){
          dot.children[0].className = 'active';
        }else{
          dot.children[pageindex].className = 'active';
        }
      }
    }
    var first_li = ul.children[0];
    var clone_first_li = first_li.cloneNode(true);
    ul.appendChild(clone_first_li);

    //左侧按钮
    var lbtn = document.querySelector('.btns a:first-child');
    lbtn.onclick = function(){
      if(pageindex == 0){
        pageindex = imgs.length;
        ul.style.left = -imgs[0].offsetWidth*pageindex + 'px';
      }
      pageindex --;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
      }
      dot.children[pageindex].className = 'active';
    }
    
    //鼠标移动到大盒子上 清除定时器停止自动播放  按钮显现出来
    var box = document.querySelector('.box');
    var btns = document.querySelector('.btns');
    var tid = null;
    box.onmouseenter = function(){
      btns.style.display = 'block';
      clearInterval(tid);
    }
    box.onmouseleave = function(){
      btns.style.display = 'none';
      //自动播放
      tid = setInterval(function(){
        rbtn.click();
      },1000);
    }

    //动画
    function animate(element,current,target,step){
      if(element.timeID){
        clearInterval(element.timeID);
        element.timeID = null;
      }
      if(current > target){
        step = -Math.abs(step)
      }
      element.timeID = setInterval(function(){
        if(Math.abs(current - target) < Math.abs(step)){
          current = target;
          clearInterval(element.timeID);
        }else{
          current += step;
        }
        element.style.left = current + 'px';
      },30);
    }
  </script>
</body>
</html>